<?php

namespace App\Http\Controllers;

use App\Exceptions\BusinessException;
use App\Http\Resources\MenuResource;
use App\Http\Services\MenuService;
use App\Http\Traits\ApiReturn;
use App\Models\Menu;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Log;
use Mews\Captcha\Facades\Captcha;

class TestController extends BaseController
{
    use ApiReturn;

    protected $service;

    public function __construct(Request $request, MenuService $menuService)
    {
        $this->service = $menuService;
        $this->request = $request;
        parent::__construct($request);
    }

    public function test()
    {

        $img = Captcha::create();
        $img = captcha_img();
        $img = app('captcha')->create('default', true);
        dd($img['img']);


//        $user = Auth::guard('api')->user();
//        $cache1 = Cache::get('user_role_perms_key_'.$user->id);
//        $cache2 = Cache::get('user_menus_key_'.$user->id);
//        dump($cache1);
//        dd($cache2);

        return $this->succ($img,'获取成功');
    }

    public function saveMenu($data)
    {
        $add = [];
        $add["parent_id"] = $data['parentId'];
        $add["name"] = $data['name'];
        $add["sort"] = $data['sort'] ?? 0;
        $add["type"] = $data['type'];
        $add["route_name"] = $data['routeName'] ?? null;
        $add["route_path"] = $data['routePath'];
        $add["component"] = $data['component'];
        $add["perm"] = $data['perm'] ?? null;
        $add["visible"] = $data['visible'];
        $add["icon"] = $data['icon'];
        $add["redirect"] = $data['redirect'] ?? null;
        $add["keep_alive"] = $data['keepAlive'] ?? 0;
        $add["always_show"] = $data['visible'] ?? 0;
        $add["params"] = json_encode($data['params'] ?? []);
        $menu = Menu::where('id',$data['id'])->first();
        if ($menu) {
            $menu->update($add);
        }else{
            $add['id'] = $data['id'];
            $menu = Menu::create($add);
        }

        if (!empty($data['children'])) {
            foreach ($data['children'] as $item) {
                $this->saveMenu($item);
            }
        }
    }


}

